import { getSrlFn } from "@/api/index.js";
import { setStorage, getStorage } from "@/baseFnMixin/localStorage/storageMixin.js";
//  避免加密文本被修改导致网页无法运行;
let storage = null;
try {
    if (window.localStorage.getItem("presentSong") !== null) {
        storage = JSON.parse(getStorage(window.localStorage.getItem("presentSong")))
    }

}
catch (err) {
    window.alert("不建议修改存储的值")
}
export default {
    state: () => ({
        //  控制是否播放音乐
        flag: true,
        //  当前播放音乐的信息 路径 时间 id
        presentSong: storage,
        //  播放时间
        playingTime: '00:00',
        //  未格式化播放时间
        playingNumberTime: 0,
        playingNumberTimeFlag: false,
        numberSumTime: null,
        audioElement: null,
        // 目前处于的模式
        PlayTheOrder: window.localStorage.getItem("order") === null ? "circulation" : window.localStorage.getItem("order"),


    }),
    actions: {
        //   获取歌曲的url
        getSongUrl: function (context, data) {

            getSrlFn(data.id).then(response => {
                data.$_songUrl = response.data.data[0].url;

                context.commit("changePresentSong", data);
            });
        }
    },

    mutations: {
        changeAudioElement: function (state, value) {
            state.audioElement = value;
        },
        PlaySong: function (state, value) {
            state.flag = value;
            if (state.presentSong.SongSrc !== null) {
                if (value) {
                    state.audioElement.pause();
                } else {
                    state.audioElement.play();
                }
            }
        },
        //  播放时间格式化
        ChangePlayingTime: function (state, value) {

            let s = Math.round(value) % 60;
            s = s > 9 ? s : `0${s}`;
            let m = null;
            if (s === "00") {
                m = Math.round(value / 60);
                m = m > 9 ? m : `0${m}`;
            } else {
                m = Math.floor(value / 60);
                m = m > 9 ? m : `0${m}`;
            }
            state.playingTime = `${m}:${s}`;

        },
        changePlayingNumberTime: function (state, value) {

            state.playingNumberTime = value;
        },
        changPlayingNumberTimeFlag: function (state, value) {

            state.playingNumberTimeFlag = value;
        },

        changePlayTheOrder(state, value) {
            //  播放顺序
            state.PlayTheOrder = value;
            window.localStorage.setItem("order", value);
        },
        // 将歌曲数据存放在presentSong  当前歌曲数据存储地
        changePresentSong(state, data) {

            let time = Math.round(data.dt / 1000);
            let s = time % 60;
            s = s > 9 ? s : `0${s}`;
            let m = Math.floor(time / 60);
            m = m > 9 ? m : `0${m}`;

            data.$_time = time;
            data.$_sumtime = `${m}:${s}`;
            state.presentSong = data;
            const storage = setStorage(JSON.stringify(state.presentSong));
            window.localStorage.setItem("presentSong", storage);

        }


    },

}



